package com.ray.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ray.bean.CombinationKeyBean;
import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.OrderBean;
import com.ray.bean.TurnoverBean;
import com.ray.service.PartsProcedureService;
import com.ray.util.ExcelExportUtil;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;
import com.ray.util.ReturnStringUtil;

/**
 * 配件工艺工序
 * @author natelu
 */
@Controller
@RequestMapping("partsProcedure")
public class PartsProcedureController {
	
     @Autowired
     PartsProcedureService partsProcedureService;
     
     /**
      * 从订单明细中获取物料类型，从公司物料表中获取对应的物料名称和材质
      * 获取完整(物料类型，物料名称，物料材质都有)的公司物料
      * @param loginCompany(公司)
      * @author natelu
      * @return
      */
 	 @RequestMapping("/getCompanyMaterial")
 	 @ResponseBody
 	 public Map getCompanyMaterial(HttpServletRequest request){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		return ResponseUtil.ResponseResult(partsProcedureService.getCompanyMaterial(map));
 	 }
 	 
 	 
 	 /**
 	  * 添加新组合
 	  * @param materialType(物料类型)
 	  * @param materialName(物料名称 可为空)
 	  * @param materialQuality(材质  可为空)
 	  * @param arts(工艺  可为空)
 	  * @param deptId(部门id)
 	  * @param procedureArray(工序json数组)
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/addCombination")
 	 @ResponseBody
 	 public Map addCombination(HttpServletRequest request){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 partsProcedureService.addCombination(map);
 		 return ResponseUtil.ResponseResult(ReturnStringUtil.ADD_SUCCESS);
 	 }
 	 
 	 
 	 /**
 	  * 通过工序另寻为添加新组合
 	  * @param materialType(物料类型)
 	  * @param materialName(物料名称 可为空)
 	  * @param materialQuality(材质  可为空)
 	  * @param arts(工艺  可为空)
 	  * @param beforeId(之前组合id)
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/saveOtherCombination")
 	 @ResponseBody
 	 public Map saveOtherCombination(HttpServletRequest request){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 partsProcedureService.saveOtherCombination(map);
 		 return ResponseUtil.ResponseResult(ReturnStringUtil.SAVE_SUCCESS);
 	 }
 	 
 	 
 	 /**
 	  * 删除组合
 	  * @param id
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/deleteCombination")
 	 @ResponseBody
 	 public Map deleteCombination(HttpServletRequest request ){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 partsProcedureService.deleteCombination(map);
 		 return ResponseUtil.ResponseResult(ReturnStringUtil.DELETE_SUCCESS);
 	 }
 	 
 	 
 	 /**
 	  * 删除多物料组合
 	  * @param id
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/deleteManyMaterialGroup")
 	 @ResponseBody
 	 public Map deleteManyMaterialGroup(HttpServletRequest request ){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 partsProcedureService.deleteManyMaterialGroup(map);
 		 return ResponseUtil.ResponseResult(ReturnStringUtil.DELETE_SUCCESS);
 	 }
 	 
 	 /**
 	  * 设置组合工序
 	  * @param id(组合id)
 	  * @param procedureArray(工序id)
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/setCombinationProcrdure")
 	 @ResponseBody
 	 public Map setCombinationProcrdure(HttpServletRequest request ){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 partsProcedureService.setCombinationProcrdure(map);
 		 return ResponseUtil.ResponseResult(ReturnStringUtil.SET_SUCCESS);
 	 }
 	 
 	 
 	 /**
 	  * 设置多物料组合工序
 	  * @param id(组合id)
 	  * @param procedureArray(工序id)
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/setManymaterialProcrdure")
 	 @ResponseBody
 	 public Map setManymaterialProcrdure(HttpServletRequest request ){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 partsProcedureService.setManymaterialProcrdure(map);
 		 return ResponseUtil.ResponseResult(ReturnStringUtil.SET_SUCCESS);
 	 }
 	 
 	 
 	 /**
 	  * 设置组合顺序
 	  * @param id(组合id)
 	  * @param firstSort(第一顺序)
 	  * @param secondSort(第二顺序)
 	  * @param thirdSort(第三顺序)
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/setCombinationSort")
 	 @ResponseBody
 	 public Map setCombinationSort(HttpServletRequest request ){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 partsProcedureService.setCombinationSort(map);
 		 return ResponseUtil.ResponseResult(ReturnStringUtil.SET_SUCCESS);
 	 }
 	 
 	 
 	 /**
 	  * 设置工序加工对象
 	  * @param maching(加工对象)
 	  * @param materialName(物料名称)
 	  * @param materialQuality(材质)
 	  * @param arts(工艺)
 	  * @param combinationId(组合id)
 	  * @param procedureId(工序id)
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/setProcedureMaching")
 	 @ResponseBody
 	 public Map setProcedureMaching(HttpServletRequest request ){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 partsProcedureService.setProcedureMaching(map);
 		 return ResponseUtil.ResponseResult(ReturnStringUtil.SET_SUCCESS);
 	 }
 	 
 	 /**
 	  * 保存订单工序
 	  * @param orderId(订单编号)
 	  * @param procedureArray(工序集合)
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/saveOrderMaterialProcedure")
 	 @ResponseBody
 	 public Map saveOrderMaterialProcedure(HttpServletRequest request ){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 partsProcedureService.saveOrderMaterialProcedure(map);
 		 return ResponseUtil.ResponseResult(ReturnStringUtil.SAVE_SUCCESS);
 	 }
 	 
 	 
 	 /**
 	  * 保存订单工序
 	  * @param orderId(订单编号)
 	  * @param procedureArray(工序集合)
 	  * @param materialType(物料类型)
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/saveUpdateProcedure")
 	 @ResponseBody
 	 public Map saveUpdateProcedure(HttpServletRequest request ){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 partsProcedureService.saveUpdateProcedure(map);
 		 return ResponseUtil.ResponseResult(ReturnStringUtil.UPDATE_SUCCESS);
 	 }
 	 
 	 
 	 /**
 	  * 获取组合物料工序
 	  * @param loginCompany(公司)
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/getMaterialProcedure")    
 	 @ResponseBody
 	 public DatatablesViewPageBean getMaterialProcedure(HttpServletRequest request){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 return partsProcedureService.getMaterialProcedure(map);
 	 }
 	 
 	 
 	 
 	 /**
 	  * 获取组合物料工序
 	  * @param loginCompany(公司)
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/getManyMaterialProcedure")    
 	 @ResponseBody
 	 public DatatablesViewPageBean getManyMaterialProcedure(HttpServletRequest request){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 return partsProcedureService.getManyMaterialProcedure(map);
 	 }
 	 
 	 
 	 /**
 	  * 获取组个没有的工序
 	  * @param id(组合id)
 	  * @param deptId(部门id)
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/getCombinationNoHaveProcrdere")    
 	 @ResponseBody
 	 public Map getCombinationNoHaveProcrdere(HttpServletRequest request){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 return ResponseUtil.ResponseResult(partsProcedureService.getCombinationNoHaveProcrdere(map));
 	 }
     
 	 /**
 	  * 获取部门下的工序
 	  * @param loginCompany(公司)
 	  * @param deptId(部门id)
 	  * @param searchWhere(查询条件)
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/getDeptProcedure")
 	 @ResponseBody
 	 public Map getDeptProcedure(HttpServletRequest request){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 return ResponseUtil.ResponseResult(partsProcedureService.getDeptProcedure(map));
 	 }
 	 
 	 
 	 /**
 	  * 获取多物料组合键下有的和没有的工序
 	  * @param id
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/getMaterialHaveAndNoHaveProcedure")
 	 @ResponseBody
 	 public Map getMaterialHaveAndNoHaveProcedure(HttpServletRequest request){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 return ResponseUtil.ResponseResult(partsProcedureService.getMaterialHaveAndNoHaveProcedure(map));
 	 }
 	 
 	 
 	 /**
 	  * 获取组合键下有的和没有的工序
 	  * @param id
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/getManyMaterialHaveAndNoHaveProcedure")
 	 @ResponseBody
 	 public Map getManyMaterialHaveAndNoHaveProcedure(HttpServletRequest request){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 return ResponseUtil.ResponseResult(partsProcedureService.getManyMaterialHaveAndNoHaveProcedure(map));
 	 }
 	 
 	 
 	 /**
 	  * 获取组合键下有的和没有的物料组合
 	  * @param id
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/getGroupHaveAndNoHaveMaterial")
 	 @ResponseBody
 	 public Map getGroupHaveAndNoHaveMaterial(HttpServletRequest request){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 return ResponseUtil.ResponseResult(partsProcedureService.getGroupHaveAndNoHaveMaterial(map));
 	 }
 	 
 	 
 	 /**
 	  * 获取订单物料组合工序
 	  * @param orderId(订单编号)
 	  * @param sort(排序字段)
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/getOrderMaterialProcedure")
 	 @ResponseBody
 	 public Map getOrderMaterialProcedure(HttpServletRequest request){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 return ResponseUtil.ResponseResult(partsProcedureService.getOrderMaterialProcedure(map));
 	 }
 	 
 	 
 	 
 	 /**
 	  * 获取已保存的订单物料工序
 	  * @param orderId(订单编号)
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/getAlreadySaveProcedure")
 	 @ResponseBody
 	 public Map getAlreadySaveProcedure(HttpServletRequest request){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 return ResponseUtil.ResponseResult(partsProcedureService.getAlreadySaveProcedure(map));
 	 }
 	 
 	 
 	 /**
 	  * 获取已保存的物料
 	  * @param loginCompany(登录人公司)
 	  * @param serachWhere(查询条件)
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/getOneMaterial")
 	 @ResponseBody
 	 public Map getOneMaterial(HttpServletRequest request){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 return ResponseUtil.ResponseResult(partsProcedureService.getOneMaterial(map));
 	 }
 	 
 	 
 	 
 	 
 	 
 	 /**
 	  * 保存多物料组合工序
 	  * @param idArray(物料组合id)
 	  * @param procedureArray(工序id,序号)
 	  * @param deptId(部门id)
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/addManyMaterial")
 	 @ResponseBody
 	 public Map addManyMaterial(HttpServletRequest request){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 partsProcedureService.addManyMaterial(map);
 		 return ResponseUtil.ResponseResult(ReturnStringUtil.ADD_SUCCESS);
 	 }
 	 
 	 
 	 /**
 	  * 删除工艺结构树
 	  * orerId (订单编号)
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/deleteTree")
 	 @ResponseBody
 	 public Map deleteTree(HttpServletRequest request){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 partsProcedureService.deleteTree(map);
 		 return ResponseUtil.ResponseResult(ReturnStringUtil.DELETE_SUCCESS);
 	 }
 	 
 	 
 	 /**
 	  * 组合物料工序另存为
 	  * @param idArray(物料组合id)
 	  * @param beforeId(保存之前的id)
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/updateGroupId")
 	 @ResponseBody
 	 public Map updateGroupId(HttpServletRequest request){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 partsProcedureService.updateGroupId(map);
 		 return ResponseUtil.ResponseResult(ReturnStringUtil.SAVE_SUCCESS);
 	 }
 	 
 	 
 	 /**
 	  * 获取订单物料
 	  * @param orderId(订单编号)
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/getOrderMaterial")
 	 @ResponseBody
 	 public Map getOrderMaterial(HttpServletRequest request){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 return ResponseUtil.ResponseResult(partsProcedureService.getOrderMaterial(map));
 	 }
 	 
 	 
 	 
 	 /**
 	  * 获取订单保存的物料类型有的和没有的
 	  * @param orderId(订单编号)
 	  * @param materialType(物料类型)
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/getOrderMaterialProcedureHaveAndNoHave")
 	 @ResponseBody
 	 public Map getOrderMaterialProcedureHaveAndNoHave(HttpServletRequest request){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 return ResponseUtil.ResponseResult(partsProcedureService.getOrderMaterialProcedureHaveAndNoHave(map));
 	 }
 	 
 	 /**
 	  * 获取订单物料中没有的工序
 	  * @param orderId(订单编号)
 	  * @param materialType(物料类型)
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/getOrderMaterialNoHave")
 	 @ResponseBody
 	 public Map getOrderMaterialNoHave(HttpServletRequest request){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 return ResponseUtil.ResponseResult(partsProcedureService.getOrderMaterialNoHave(map));
 	 }
 	 
 	 
 	 /**
 	  * 获取订单物料采购和入库时间
 	  * @param orderId(订单编号)
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/getOrderMaterialParchaseAndInCountTime")
 	 @ResponseBody
 	 public Map getOrderMaterialParchaseAndInCountTime(HttpServletRequest request){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 return ResponseUtil.ResponseResult(partsProcedureService.getOrderMaterialParchaseAndInCountTime(map));
 	 }
 	 
 	 
 	 /**
 	  * 获取工序剩余需要生产的数量
 	  * @param procedureId (工序id)
 	  * @author natelu
 	  * @return
 	  */
 	 @RequestMapping("/getProcedureSurplusCount")
 	 @ResponseBody
 	 public Map getProcedureSurplusCount(HttpServletRequest request){
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 return ResponseUtil.ResponseResult(partsProcedureService.getProcedureSurplusCount(map));
 	 }
 	 
 	 
 	 
 	 
 	 /**
 	  * 订单工序导出为excle
 	  * @param orderId(订单编号)
 	  * @param searchType(是查询还是排序  sort query)
 	  * @author natelu
 	  * @return
 	 * @throws IOException 
 	  */
 	 @RequestMapping("/downlodeOrderPrucedureData")
 	 @ResponseBody
 	 public void downlodeOrderPrucedureData(HttpServletRequest request,HttpServletResponse response) throws IOException{
 		Map map=RequestUtil.getRequestAllParam(request);
 		String tempFolder=request.getServletContext().getRealPath("/")+"tempExcelFolder";
 		File excelFile=partsProcedureService.createExcelFile(map,tempFolder);
 		ResponseUtil.downloadfile(response, excelFile, "订单配件工艺.xlsx", true);
 	 }
 	 
 	 
 	 /**
 	  * 配件工序导出为excle
 	  * @param searchWhere(是查询还是排序  sort query)
 	  * @author natelu
 	  * @return
 	  * @throws IOException 
 	  */
 	 @RequestMapping("/downlodeCombinationKeyPrucedureData")
 	 @ResponseBody
 	 public void downlodeCombinationKeyPrucedureData(HttpServletRequest request,HttpServletResponse response) throws IOException{
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 String tempFolder=request.getServletContext().getRealPath("/")+"tempExcelFolder";
 		 File excelFile=partsProcedureService.createCombinationKeyPrucedurExcelFile(map,tempFolder);
 		 ResponseUtil.downloadfile(response, excelFile, "配件工艺.xlsx", true);
 	 }
 	 
 	 
 	 /**
 	  * 组合配件工序导出为excle
 	  * @author natelu
 	  * @return
 	  * @throws IOException 
 	  */
 	 @RequestMapping("/downlodeManyCombinationKeyPrucedureData")
 	 @ResponseBody
 	 public void downlodeManyCombinationKeyPrucedureData(HttpServletRequest request,HttpServletResponse response) throws IOException{
 		 Map map=RequestUtil.getRequestAllParam(request);
 		 String tempFolder=request.getServletContext().getRealPath("/")+"tempExcelFolder";
 		 File excelFile=partsProcedureService.downlodeManyCombinationKeyPrucedureData(map,tempFolder);
 		 ResponseUtil.downloadfile(response, excelFile, "组合配件工艺.xlsx", true);
 	 }
 	 
// 	 /**
// 	  * 组合配件工序导出为excle
// 	  * @author natelu
// 	  * @return
// 	  * @throws IOException 
// 	 * @throws IllegalAccessException 
// 	 * @throws IllegalArgumentException 
// 	 * @throws SecurityException 
// 	 * @throws NoSuchFieldException 
// 	  */
// 	 @RequestMapping("/downlodeProcedureSurplusCount")
// 	 @ResponseBody
// 	 public void downlodeProcedureSurplusCount(HttpServletRequest request,HttpServletResponse response) throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
// 		 Map map=RequestUtil.getRequestAllParam(request);
// 		List<OrderBean> orderBeans = partsProcedureService.getProcedureSurplusCount(map);
//		orderBeans.forEach(order->{
//			order.setDuanChu(order.getFeedCount()-order.getQuality());
//		});
//		String [] tableHeadFields={"订单编号","系统订单编号","工厂型号","客户型号","客户订单号","订单数","投料数","已生产数","待生产数"};
//		String [] fields={"companyOrderId","orderId","factoryId","customerId","customerOrderId","orderCount","feedCount","quality","duanChu"};
//		String tempFolder=request.getServletContext().getRealPath("/")+"tempExcelFolder";
//		File excelFile=ExcelExportUtil.createExcelFile(orderBeans,tempFolder,tableHeadFields,fields);
//		ResponseUtil.downloadfile(response, excelFile, "工序待生产数量.xlsx", true);
// 	 }
}
